package com.example.sulei.rootchangerinfo;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sulei.rootchangerinfo.utils.PermissionsUtils;

public class MainActivity extends AppCompatActivity {

    private TextView test;
    private EditText imei;
    private EditText mac;
    private TelephonyManager service;
    private WifiManager wifiservice;
    private Spinner brand;
    private Spinner model;
    ArrayAdapter<String> adapter;
    ArrayAdapter<String> adapter2;

    private String[] province = new String[]{"直辖市", "特别行政区", "黑龙江"};
    private String[] city = new String[]{"北京", "上海", "天津", "重庆"};
    private String[][] pandc = new String[][]{{"北京", "上海", "天津", "重庆"}, {"香港", "澳门"}, {"哈尔滨", "齐齐哈尔", "牡丹江", "大庆", "伊春", "双鸭山", "鹤岗", "鸡西", "佳木斯", "七台河", "黑河", "绥化", "大兴安岭"}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        应用程序申请获取Root权限
        PermissionsUtils.upgradeRootPermission(getPackageCodePath());
        initView();

//        test.setText("是否已获取Root权限==" + RootTools.isRootAvailable() + "RootTools.isAccessGiven()==" + RootTools.isAccessGiven());
        service = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        wifiservice = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

    }

    private void initView() {
        test = (TextView) findViewById(R.id.tv_test);
        imei = (EditText) findViewById(R.id.et_imei);
        mac = (EditText) findViewById(R.id.et_mac);
        brand = (Spinner) findViewById(R.id.sp_brand);
        model = (Spinner) findViewById(R.id.sp_model);
        findViewById(R.id.bt_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //setDate();
            }
        });
        setSpinner();
    }

    private void setSpinner() {
        String[] brands = new String[]{"小米", "华为", "OPPO", "Vivo"};
        String[][] models = new String[][]{{"小米3", "小米4", "小米5"}, {"小米3", "小米4", "小米5"}, {"小米3", "小米4", "小米5"}, {"小米3", "小米4", "小米5"}};
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, province);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        brand.setAdapter(adapter);

        adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        model.setAdapter(adapter2);

        AdapterView.OnItemSelectedListener selectListener = new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView parent, View v, int position, long id) {
                int pos = brand.getSelectedItemPosition();
                adapter2 = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, pandc[pos]);
                model.setAdapter(adapter2);
            }

            public void onNothingSelected(AdapterView arg0) {

            }

        };
        brand.setOnItemClickListener((AdapterView.OnItemClickListener) selectListener);
    }


    @Override
    protected void onResume() {
        super.onResume();
        //读取当前设备的IMEI
        test.setText("当前设备的IMEI:" + service.getDeviceId() + "\n MAC地址：" + wifiservice.getConnectionInfo().getMacAddress()
                + "\n 品牌：" + Build.BRAND + "\n型号：" + Build.MODEL);
    }


    private void setDate() {
        SharedPreferences sh = this.getSharedPreferences("prefs", Context.MODE_WORLD_READABLE);
        SharedPreferences.Editor pre = sh.edit();
        pre.putString("imei", imei.getText().toString());
        pre.putString("mac", mac.getText().toString());
        pre.apply();
        Toast.makeText(MainActivity.this, "修改iemi====" + imei.getText().toString(), Toast.LENGTH_SHORT).show();

//        SharedPreferences spf = getSharedPreferences("spf", Activity.MODE_PRIVATE);
//        SharedPreferences.Editor edit = spf.edit();
//        edit.putString("imei", imei.getText().toString());
//        edit.putString("mac", mac.getText().toString());
//        edit.commit();
    }

}
